set more off
use "C:\Users\Andy Baker\OneDrive - UCB-O365\Data\Brazil\BEPS 2014\BEPS 2014 Merged Data v3.dta", clear

xtset idtelefone wave
gen respond=1

*Before reshaping, generate some variables
	*vote choice
recode vb1 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=15 11=11 12=. 13=17 14=17 .a=16 .b=. .=., gen(vote)
label define vote 1 Dilma 2 Aecio 3 Campos 4 Marina 5 "Everaldo Pereira" 6 "Eduardo Jorge" 7 "Luciana Genro" 11 "Levy Fidelix" 15 Other 17 Abstain 16 DK
label values vote vote

	*Number of discussacnts
replace disc5b=. if disc5a==.a | disc5a==.b | disc5a==.c | disc5a==.
replace disc6b=. if disc6a==.a | disc6a==.b | disc6a==.c | disc6a==.

recode disc5a 1/8=1 9/10=0, gen(disc_family)
recode disc6a 1/8=1 10=0, gen(disc_friend)
egen disc_total=rsum(disc_family disc_friend)
replace disc_total=. if wave==1 | wave==6 | wave==7

	*Discussion frequency
recode disc1-disc3 (1=4) (2=3) (3=2) (4=1) (.=.)
rename disc1 discfam
rename disc2 discfriends

	*Media exposure
recode med1 (1=45) (2=30) (3=14) (4=4) (5=.5), gen(mediaattention)

*educ
replace educ=ln(educ)

*male
recode mulher 0=1 1=0, gen(male)

*race
recode p2 1=3 2=2 3=1 4=1 5=2, gen(racemean)

rename l1 ideo


keep idtelefone wave respond vote disc_* discfam discfriends mediaattention weight_combined wealth hw* p16 educ male race pk* ideo ft*
reshape wide respond vote disc_* discfam discfriends mediaattention weight_combined wealth hw* p16 educ male race pk* ideo ft*, i(idtelefone) j(wave)
svyset [pweight=weight_combined1]

* Political awareness
*egen awarez=std(pkw11)
	*I added in ideo and ft because the set of pk items is severely truncated at the bottom. 43% didn't know any.
recode pk11 pk21 pk31 pk41 pk51 (.a=0) (.b=0) (2=0)
recode ideo1 (0/10=1) (.a=0) (.b=0) (.=.), gen(ideo_respond1) 
recode ft11-ft71 (0/10=1) (.a=0) (.b=0) (.=.)
egen ft_know=anycount(ft11 ft21 ft31 ft41 ft51 ft61 ft71), values(1)
svy: irt grm pk11 pk21 pk31 pk41 pk51 ideo_respond1 ft_know
predict aware_irt, latent ebmeans
egen awarez=std(aware_irt)

egen conversemean=rowmean(discfam1 discfriends1)
egen mediaattention=rowmean(mediaattention2 - mediaattention6)
egen disctotalmean=rowmean( disc_total2 disc_total3 disc_total4 disc_total5)

*Wealth
drop wealth*
recode hw11 hw21 hw31 hw41 hw51 hw61 p161 (2=0)
irt grm hw*1 p161
predict wealth1, latent ebmeans

*Start regs
egen mediaattention_std=std(mediaattention)
egen disctotalmean_std=std(disctotalmean)
egen conversemean_std=std(conversemean)

label variable wealth1 "Wealth"
label variable educ1 "Education level"
label variable male1 "Male"
label variable racemean1 "Race"
label variable mediaattention_std "Frequency of TV news viewing"
label variable conversemean_std "Frequency of political discussion"
label variable disctotalmean_std "Egocentric network size"

*Table A.21
eststo clear
eststo: svy: reg conversemean_std wealth1 educ1 male1 racemean1
margin, at((p5) wealth1 educ1) at((p95) wealth1 educ1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth1=r(b)
mat vwealth1=r(V)
margin, at((p5) male1) at((p95) male1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex1=r(b)
mat vsex1=r(V)
margin, at((p5) racemean1) at((p95) racemean1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace1=r(b)
mat vrace1=r(V)

eststo: svy: reg disctotalmean_std wealth1 educ1 male1 racemean1
margin, at((p5) wealth1 educ1) at((p95) wealth1 educ1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth2=r(b)
mat vwealth2=r(V)
margin, at((p5) male1) at((p95) male1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex2=r(b)
mat vsex2=r(V)
margin, at((p5) racemean1) at((p95) racemean1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace2=r(b)
mat vrace2=r(V)

eststo: svy: reg mediaattention_std wealth1 educ1 male1 racemean1
margin, at((p5) wealth1 educ1) at((p95) wealth1 educ1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth3=r(b)
mat vwealth3=r(V)
margin, at((p5) male1) at((p95) male1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex3=r(b)
mat vsex3=r(V)
margin, at((p5) racemean1) at((p95) racemean1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace3=r(b)
mat vrace3=r(V)

mat bwealth4=.
mat vwealth4=.
mat bsex4=.
mat vsex4=.
mat brace4=.
mat vrace4=.

mat bwealth5=.
mat vwealth5=.
mat bsex5=.
mat vsex5=.
mat brace5=.
mat vrace5=.

mat Bwealth=[bwealth5\bwealth4\bwealth3\bwealth2\bwealth1]
mat Bsex=[bsex5\bsex4\bsex3\bsex2\bsex1]
mat Brace=[brace5\brace4\brace3\brace2\brace1]

mat Vwealth=[vwealth5\vwealth4\vwealth3\vwealth2\vwealth1]
mat Vsex=[vsex5\vsex4\vsex3\vsex2\vsex1]
mat Vrace=[vrace5\vrace4\vrace3\vrace2\vrace1]

svmat Bwealth 
svmat Bsex 
svmat Brace 
svmat Vwealth 
svmat Vsex 
svmat Vrace
gen nwealth=_n+.125
gen nsex=_n
gen nrace=_n-.125
gen n=_n
gen wealthupper=Bwealth+1.9645*(Vwealth^.5)
gen wealthlower=Bwealth-1.9645*(Vwealth^.5)
gen sexupper=Bsex+1.9645*(Vsex^.5)
gen sexlower=Bsex-1.9645*(Vsex^.5)
gen raceupper=Brace+1.9645*(Vrace^.5)
gen racelower=Brace-1.9645*(Vrace^.5)

*Figure 8.1C
twoway (rcap wealthupper wealthlower nwealth if n<6, color(black) horizontal ) (scatter nwealth Bwealth if n<6, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<6, color(black) horizontal ) (scatter nsex Bsex if n<6, mcolor(black) msymbol(S) msize(medsmall)) ///
(rcap raceupper racelower nrace if n<6, color(black) horizontal ) (scatter nrace Brace if n<6, mcolor(black) msymbol(t) msize(medlarge)) , ///
xtitle("{bf:Model-Predicted Change in DV}" "(upon changing SES, gender, or race" "from min to max)", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Dependent Variables (DV):}" "{bf:Exposure to Political Intermediaries}", size(medium)) ///
legend(off) xscale(range(-.5 2.2)) ///
xline(0, lcolor(black)) ylab(5 "Freq. of pol. discussion" 4 "Egocentric network size"  3 "Media exposure" 2 "Contacted by a party" 1 "Paid off by a party", angle(horizontal) labsize(medium)) ///
ylab(,nogrid) xlab(-.5 0 .5 1 1.5 2.0, grid) yline(1.5 2.5 3.4 4.5, lcolor(black))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Brazil 2014.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1C.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1C.pdf", as(pdf) replace 

*legend(order(2 "wealth and education" 4 "gender (male)" 6 "race (white)") region(lcolor(white)) colgap(20) col(1) color(black)) ///

*for appendix
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Output\"
esttab est1 est2 est3 using Brazil2014strat.rtf, label b(3) replace se star(* 0.10 ** 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Brazil 2014)

*Figure 8.3, Table A.23
svy: reg awarez wealth1 educ1 male1 
margin, at((p5) wealth1 educ1) at((p95) wealth1 educ1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthBR14=r(b)
mat vwealthBR14=r(V)
margin, at((p5) male1) at((p95) male1) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexBR14=r(b)
mat vsexBR14=r(V)
*esttab est1 using Brazil2014strataware.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(awareness Brazil 2014)

